<?php

class controller_pk
{
	private $m_current_period = null;
	
	public function __construct() {
		require BASEPATH.'model/model_pk.php';
		$this->pk = new model_pk();
	}
	private function get_current_period() {
		require_once (BASEPATH . 'library/date_helper.php');
		$current_time = now ();
		$query = $this->pk->get_current_period();
		if ($query != null) {

			if ($current_time < strtotime($query->start_time) ) {
				log_message('error', 'current_time should not less than start_time');
			}
			else if ($current_time > strtotime($query->end_time) ) { //should change period
				if (!$this->out_of_time()) {
					$query = null;
				}
			}
		}
		
		if ($query == null) {
			$start_time = null;
			$end_time = null;
			$h = strftime ( '%H', $current_time );
			if ($h >= 16) {
				$start_time = $current_time;
				$end_time = strtotime ( "+1 day", $current_time );
				//$end_time = $start_time;
			}
			else {
				$start_time = strtotime ( "-1 day", $current_time );
				$end_time = $current_time;
			}
			$y = strftime ( '%Y', $start_time );
			$m = strftime ( '%m', $start_time );
			$d = strftime ( '%d', $start_time );
			$this->m_current_period->start_time = strftime ( "%Y-%m-%d %H:%M:%S", mktime ( 16, 0, 0, $m, $d, $y ) );
			$y = strftime ( '%Y', $end_time );
			$m = strftime ( '%m', $end_time );
			$d = strftime ( '%d', $end_time );
			$this->m_current_period->period = intval(sprintf ( '%s%s%s', $y, $m, $d)) ;
			$this->m_current_period->end_time = strftime ( "%Y-%m-%d %H:%M:%S", mktime ( 12, 59, 59, $m, $d, $y ) );
			$this->m_current_period->number = 0;
			
			$this->pk->add_new_pk_period( $this->m_current_period );
			$this->pk->pk_change_period($this->m_current_period->period);
			}
		else {
			$this->m_current_period = $query;
		}
		log_message ( 'debug', 'get_current_period period: ' . $this->m_current_period->period . ' start_time: ' . $this->m_current_period->start_time . ' end_time: ' . $this->m_current_period->end_time );
		return true;
	}
	
	private function out_of_time() {
		require_once (BASEPATH . 'library/date_helper.php');
		$current_time = now ();
		$h = strftime ( '%H', $current_time );
		if ($h >= 13 && $h < 16) {
			return true;
		}
		else {
			return false;
		}
	}
	public function upload_pk_dress() {
		$result = null;
		$result->return_code = 0;
		$result->protocal = get_protocal( $_GET ['protocal'] );
		$userid = $_GET ['userid'];
		
		if ($this->out_of_time()) {
			$result->return_code = 30001;
			goto label_return;
		}
		if ($this->pk->is_pk_exist ( $userid )) {
			$result->return_code = 30002;
			goto label_return;
		}

		$username = $_POST['username'];
		$level = $_GET['level'];
		$period = $_GET['period'];
		
		$plist = $_POST ['plist'];
		$md5sum = $_POST ['md5sum'];

		require_once (BASEPATH.'library/date_helper.php');
		$time = strftime ( "%Y-%m-%d %H:%M:%S", now () );
		$return = $this->pk->add_pk ( $userid, $username, $level, $period, $time, $plist, $md5sum );
		
		if (! $return) {
			$result->return_code = 1;
			goto label_return;
		}
		$query = $this->pk->get_pk_by_userid($userid);
		if ( mysql_num_rows($query) > 0 ) {
			$result->pk_dress = mysql_fetch_object($query);
		}
		
	label_return:
		echo json_encode ( $result );
	}
	
	public function get_current_period_pk_list() {
		$result = null;
		$result->return_code = 0;
		$result->protocal = get_protocal( $_GET ['protocal'] );
		
		$userid = $_GET['userid'];
		$start_page = $_GET ['start_page'];
		$num_per_page = $_GET ['num_per_page'];
		$num_page = $_GET ['num_page'];
		
		$start = $start_page * $num_per_page;
		$num = $num_page * $num_per_page;
		$query = $this->pk->get_pk_list_by_score ( $start, $num );
		
		while ($row = mysql_fetch_object($query))
		{
			$result->pk_list[] = $row;
		}
		echo json_encode ( $result );
	}
	
	public function get_current_period_rank_list() {
		$result = null;
		$result->return_code = 0;
		$result->protocal = get_protocal( $_GET ['protocal'] );
		
		$query = $this->pk->get_rank_list ( );
		
		while ($row = mysql_fetch_object($query))
		{
			$result->rank_list[] = $row;
		}
		echo json_encode ( $result );
	}
	public function pk_vote() {
		$result = null;
		$result->return_code = 0;
		$result->protocal = get_protocal( $_GET ['protocal'] );
		
		if ($this->out_of_time()) {
			$result->return_code = 30001;
			goto label_return;
		}
		
		$userid = $_GET['userid'];
		require_once (BASEPATH.'library/date_helper.php');
		$time = strftime ( "%Y-%m-%d %H:%M:%S", now () );
		if (!$this->pk->pk_vote ( $userid, $time )) {
			$result->return_code = 1;
		}
		$result->userid = $userid;

	label_return:
		echo json_encode( $result );
	}
	
	public function send_pk_result() {
		$result = null;
		$result->return_code = 0;
		$result->protocal = get_protocal( $_GET ['protocal'] );
		
		$user1 = $_GET['selfid'];
		$score1 = $_GET['self_score'];
		$user2 = $_GET['otherid'];
		$score2 = $_GET['other_score'];
		
		if ($this->out_of_time()) {
			$result->return_code = 30001;
			goto label_return;
		}
		
		if (!$this->pk->pk_change_score( $user1, $score1, $user2, $score2 ) ) {
			$result->return_code = 1;
		}
//		$query = $this->pk->get_pk_base_by_userid($user1);
//		if (mysql_num_rows($query) > 0) {
//			$result->self_info = mysql_fetch_object($query);
//		}
//		$query = $this->pk->get_pk_base_by_userid($user2);
//		if (mysql_num_rows($query) > 0) {
//			$result->other_info = mysql_fetch_object($query);
//		}

	label_return:
		echo json_encode( $result );
	}
	
	// 自己pk本期作品，粉丝数量，前面几期还没有领取的奖励
	public function get_pk_global_info() {
		$result = null;
		$result->return_code = 0;
		$result->protocal = get_protocal( $_GET ['protocal'] );
		
		$version = floor( $_GET['version'] );
		if ($version < FASHION_VERSION) {
			$result->return_code = 10001;
			goto label_return;
		}
		$userid = $_GET['userid'];
		
		$this->get_current_period();
		$result->cg_current_period = $this->m_current_period;
		
		require_once (BASEPATH.'library/date_helper.php');
		$time = strftime ( "%Y-%m-%d %H:%M:%S", now () );
		$result->current_time = $time;
		
		$query = $this->pk->get_pk_by_userid($userid);
		if ( mysql_num_rows($query) > 0 ) {
			$result->pk_dress = mysql_fetch_object($query);
		}
		
		$query2 = $this->pk->get_pk_global($userid);
		if ($query2 != null) {
			$result->global = $query2;
		}
		// get last reward
		$query = $this->pk->get_pk_reward($userid);
		while ($row = mysql_fetch_object($query))
		{
			$result->reward_list[] = $row;
		}
	label_return:
		echo json_encode( $result );
	}
	
	public function confirm_receive_reward() {
		$result = null;
		$result->return_code = 0;
		$result->protocal = get_protocal( $_GET ['protocal'] );
		
		$userid = $_GET['userid'];
		$period = $_GET['period'];
		$ret = $this->pk->get_pk_reward_state($userid, $period);
		if ($ret == null) {
			$result->return_code = 30005;
		}
		else if ($ret == 1){
			$result->return_code = 30004;
		}
		else {
			if (!$this->pk->pk_has_get_reward ( $userid, $period )) {
				$result->return_code = 1;
			}
		}
		
		echo json_encode( $result );
	}
}

if (IS_MAINTAINING) {
	$result = null;
	$result->return_code = 10002;
	echo json_encode($result);
}
else {
$protocal = get_protocal( $_GET['protocal'] );
log_message('debug', "enter protocal $protocal");

$controller_pk = new controller_pk();
switch ($protocal) {
	case 2201 :

		$controller_pk->upload_pk_dress();
		break;
	case 2202 :
		$controller_pk->get_current_period_pk_list();
		break;
	case 2203 :
		$controller_pk->get_pk_global_info();
		break;
	case 2204 :
		$controller_pk->pk_vote();
		break;
	case 2205 :
		$controller_pk->send_pk_result();
		break;
	case 2206 :
		$controller_pk->confirm_receive_reward();
		break;
	case 2207 :
		$controller_pk->get_current_period_rank_list();
		break;
	default :
		break;
}
}
